#include "stdTcp.h"
#include <stdio.h>
//#include "../GlobalMessage.h"
#include "StdThread.h"// 客户端加线程
#include "ClientFunc.h"
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <unistd.h>



#define ip "172.29.0.125"
#define port 8080

static char MyName[NameSize];


// 解析传回来的消息
void AnalysisRecv(MSG * m)
{
    switch (m->flag)
    {
    case CHAT:
    case BROADCAST:
    {
        printf("接收到来自[%s]的消息：%s\n", m->FromName, m->Content);
        break;
    }
    case ONLINELIST:
        ShowOnlineList(m);
        break;
    case SENDFILE:
        WriteToLocal(m);
        break;
    case UPLOAD:
    {
        // 读写分离：主线程写，子线程读 千万不能冲突：时间一长会出现竞争
        if (strcmp(m->Content, "UPDATEOK") == 0)
        {
            printf("上传文件成功\n");
        }
        break;
    }
    case FILELIST:
    {
        // 显示文件列表
        ShowFileList(m);
        break;
    }
    default:
        break;
    }
}

// 专门处理来自服务器的消息
void * ThreadHandle(void * arg)
{
    // 里面需要收到消息
    TcpC *c = (TcpC *)arg;
    while(1)
    {
        MSG m;
        if (TcpClientRecv(c, &m, sizeof(m)) == false)
        {
            printf("读线程接收错误\n");
            exit(-1);// 直接退掉整个进程
        }
        AnalysisRecv(&m);

    }    
}

void LoginSignUp(TcpC *c)
{
    while (1)
    {
        int choice = LoginFace();
        MSG m;
        switch (choice)
        {
        case LOGIN:
            m = Login(MyName);
            TcpClientSend(c, &m, sizeof(MSG));
            if (TcpClientRecv(c, &m, sizeof(MSG)) == false)
            {
                exit(-1);
            }
            if (IsLoginSuccess(&m) == true)
            {
                return;
            }
            break;
        case SIGUP:
            m = SignUp(MyName);
            TcpClientSend(c, &m, sizeof(MSG));
            if (TcpClientRecv(c, &m, sizeof(MSG)) == false)
            {
                exit(-1);
            }
            if (IsSignUpSuccess(&m) == true)
            {
                return;
            }
            break;
        default:
            break;
        }
        sleep(2);
        system("clear");
    }
}

// 把文件一包一包发送给服务器
void SendFileToServer(MSG * m, TcpC *c)
{
    char * fileName = GetFileNameFromPath(m->Content);
    PacketFile *pf = PacketFileInit(m->Content);
    memset(m->Content, 0, sizeof(m->Content));
    strcpy(m->Content + 4, fileName);
    int size = 1000;
    int count = 0;
    while(IsFileEnd(pf) == false)
    {
        ReadFromFile(pf, m->Content + 24, &size);
        // 填入读取的文件内容大小
        memcpy(m->Content, &size, sizeof(int));

        TcpClientSend(c, m, sizeof(MSG));

        count++;
        printf("msg flag : %d 这是第%d包数据,大小:%d\n", m->flag, count, size);
    }

    // 传结束的空包
    size = -1;
    memcpy(m->Content, &size, sizeof(int));
    TcpClientSend(c, m, sizeof(MSG));// 发送空包
    free(fileName);
    ClearPacketFile(pf);
}


int main()
{
    TcpC *c = TcpClientInit(ip, port);

    LoginSignUp(c);

    // 在注册之后开这条线程，才有这条信息
    Thread *t = ThreadInit(ThreadHandle, c);

    while (1)
    {
        int choice = Interface();
        MSG m;

        switch (choice)
        {
        case CHAT:// 私聊
            m = ChatToClient();
            strcpy(m.FromName, MyName);
            TcpClientSend(c, &m, sizeof(m));
            // if (TcpClientSend(c, &m, sizeof(m)) == false)
            // {
            //     perror("send error");
            //     return -1;
            // }
            break;
        // 广播
        case BROADCAST:// 群聊
            m = ChatBroadCast();
            strcpy(m.FromName, MyName);
            TcpClientSend(c, &m, sizeof(m));
            break;
        // 在线用户
        case ONLINELIST:
            m = GetServerOnlineList();
            strcpy(m.FromName, MyName);
            TcpClientSend(c, &m, sizeof(m));
            break;
        // 发送文件
        case SENDFILE:
            m = SendFile();
            strcpy(m.FromName, MyName);
            SendFileToServer(&m, c);

            break;
        case MYCLOUD:
            while(1)
            {
                int choice = CloudInitface();
                switch (choice)
                {
                    case FILELIST:
                    {   
                        strcpy(m.FromName, MyName);
                        m.flag = FILELIST;
                        TcpClientSend(c, &m, sizeof(m));
                        break;
                    }
                    case UPLOAD:
                    {
                        m = UploadFile();
                        strcpy(m.FromName, MyName);
                        SendFileToServer(&m, c);
                        break;
                    } 
                    case DOWNLOAD:
                    {
                        break;
                    }
                    default:
                    {
                        break;
                    }
                }
            }
            break;
        default:
            break;
        }     
    }

    return 0;
}
